package com.mvd.app.ac2019;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.activity.WaiJianPhotoActivity;
import com.mvd.app.adapter.MyProvincesListAdapter2;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.Constants;
import com.mvd.app.commons.DateUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.commons.VehicleCheckUtils;
import com.mvd.app.model.CarItemStartModel;
import com.mvd.app.model.CarsInforModel;
import com.mvd.app.model.UploadPhotoModel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.mvd.app.application.BaseApplication.WAIJIAN_PHOTO_PREVIEW_LISTS;

/**
 * 公共页面
 * <p>
 * 新版   车辆详情 页面
 */

public class VehicleDetailsActivity extends BaseActivity {

    public VehicleDetailsActivity instances = null;
    @BindView(R.id.title_btn_left)
    ImageButton titleBtnLeft;
    @BindView(R.id.title_btn_left_txt)
    Button titleBtnLeftTxt;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.iv_headtip)
    ImageView ivHeadtip;
    @BindView(R.id.title_btn_right_txt)
    Button titleBtnRightTxt;
    @BindView(R.id.title_btn_right)
    ImageButton titleBtnRight;
    @BindView(R.id.ac_vehicle_number)
    TextView acVehicleNumber;
    @BindView(R.id.ac_vehicle_times)
    TextView acVehicleTimes;
    @BindView(R.id.ac_vehicle_line)
    TextView acVehicleLine;
    @BindView(R.id.ac_vehicle_btn_cancle)
    Button acVehicleBtnCancle;
    @BindView(R.id.ac_login_view_m_vehicle)
    TextView acLoginViewMVehicle;
    @BindView(R.id.ac_vehicle_btn_start)
    Button acVehicleBtnStart;
    @BindView(R.id.ac_vehicle_car_color)
    TextView acVehicleCarColor;

    private int CHECK_MODE = 0;//检测模式 0:外检  1：路试 2：底盘动态
    private String Line = "1";//外检车道
    private String Item_Code = "";//F1外检,R1路试，DC底盘动态
    private String Login_name = "", login_IDNumber;//当前登录的检测员的名字
    private CarsInforModel carsInforModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_vehicle_details);
        ButterKnife.bind(this);

        setBackBtn();
        setTopTitle("车辆详情");
        instances = this;
        pop_xuhao();
        carsInforModel = (CarsInforModel) getIntent().getSerializableExtra("model_CarsInforModel");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        Login_name = getIntent().getExtras().getString("Login_name", "");
        CHECK_MODE = BaseApplication.JIANCE_MODE;
        switch (CHECK_MODE) {
            case 0:
                Item_Code = "F1";
                break;
            case 1:
                Item_Code = "R1";
                break;
            case 2:
                Item_Code = "DC";
                break;
        }
//        String logStr = "\n" + "外检--车辆详情----" +
//                "\n" + "carsInforModel::" + carsInforModel.toString() + "\n" + "login_IDNumber::" + login_IDNumber + "\n" + "Login_name::" + Login_name +
//                "\n" + "CHECK_MODE::" + CHECK_MODE;
//        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        if (BaseApplication.checkItemModelList != null) {
            String logStr2 = "\n" + "外检--车辆详情页面-BaseApplication.checkItemModelList" +
                    "\n" + "BaseApplication.checkItemModelList::" + BaseApplication.checkItemModelList.toString() + "\n";
            PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
        } else {
            String logStr2 = "\n" + "外检--车辆详情页面-BaseApplication.checkItemModelList=null" + "\n";
            PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
        }
        if (carsInforModel != null) {
            //车牌号码
            if (!TextUtils.isEmpty(carsInforModel.getPlateNO())) {
                acVehicleNumber.setText(carsInforModel.getPlateNO());
            }
            //检验次数```````
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getTimes()))) {
                acVehicleTimes.setText(carsInforModel.getTimes() + "次");
            }
//            //车身颜色
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getBodyColor()))) {
                acVehicleCarColor.setText(VehicleCheckUtils.CarscolorToString(carsInforModel
                        .getBodyColor()));
            }
        }
    }

    @OnClick({R.id.ac_vehicle_number, R.id.ac_vehicle_times, R.id.ac_vehicle_line,
            R.id.ac_vehicle_btn_cancle, R.id.ac_vehicle_btn_start})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_vehicle_number:
                break;
            case R.id.ac_vehicle_times:
                break;
            case R.id.ac_vehicle_line:
                pop_xuhao.showAsDropDown(acVehicleLine);
                listview_xuhao.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int position, long arg3) {
                        acVehicleLine.setText(xuhaoList.get(position));
                        Line = xuhaoList.get(position);
                        pop_xuhao.dismiss();
                    }
                });
                break;
            case R.id.ac_vehicle_btn_cancle:
                instances.finish();
                break;
            case R.id.ac_vehicle_btn_start:
                //项目开始
                if (!TextUtils.isEmpty(Item_Code)) {
                    if (CHECK_MODE == 0) {
                        getDataWaiJainStart("");
                        String logStr = "车牌号码：：" + carsInforModel.getPlateNO() + "\n" + "动作：点击项目开始按钮";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                    } else {

                    }

                }
                break;
        }
    }

    //popuwindow
    private View popu_xuhao_view;
    private LinearLayout parent22;
    private PopupWindow pop_xuhao = null;
    private ListView listview_xuhao;
    private MyProvincesListAdapter2 myProAdapter;
    private List<String> xuhaoList = new ArrayList<String>();

    private void pop_xuhao() {
        for (int i = 0; i < Constants.WAIJIAN_CHEDAO_CHEJIAN_XIANHAO.length; i++) {
            xuhaoList.add(i, Constants.WAIJIAN_CHEDAO_CHEJIAN_XIANHAO[i]);
        }
        pop_xuhao = new PopupWindow(this);
        popu_xuhao_view = getLayoutInflater().inflate(R.layout.item_setting_province_letter,
                null);
        pop_xuhao.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        pop_xuhao.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        pop_xuhao.setBackgroundDrawable(new BitmapDrawable());
        pop_xuhao.setFocusable(true);
        pop_xuhao.setOutsideTouchable(true);
        pop_xuhao.setContentView(popu_xuhao_view);
        parent22 = (LinearLayout) popu_xuhao_view.findViewById(R.id.setting_provinces_parent);
        listview_xuhao = (ListView) popu_xuhao_view.findViewById(R.id.setting_provinces_listview2);
        myProAdapter = new MyProvincesListAdapter2(this, xuhaoList);
        listview_xuhao.setAdapter(myProAdapter);
        parent22.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pop_xuhao.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        instances = null;
    }

    //外检检测开始
    private void getDataWaiJainStart(String currentTime) {
        CarItemStartModel carItemStartModel = new CarItemStartModel();
        carItemStartModel.setPlatformSN(carsInforModel.getPlatformSN());
        carItemStartModel.setPlateType(carsInforModel.getPlateType());
//        carItemStartModel.setPlateNO(carsInforModel.getPlateNO());
        carItemStartModel.setPlateNO(carsInforModel.getPlateNO());
        carItemStartModel.setSN(carsInforModel.getSN());
        carItemStartModel.setTimes(carsInforModel.getTimes());
//        data.get(position).getPlateRegion() + data.get(position).getPlateNO()
        carItemStartModel.setVIN(carsInforModel.getVIN());
        carItemStartModel.setDetectionID(String.valueOf(carsInforModel.getID()));
        carItemStartModel.setLine(Line);
        carItemStartModel.setTest_times(carsInforModel.getTimes());
        carItemStartModel.setDetectionDevID(SharedPreferencesUtils.getIMEI(this));
        carItemStartModel.setItemCode(Item_Code);
        carItemStartModel.setAppearanceInspector(Login_name);
        BaseApplication.W_ItemStart_time = "";
        BaseApplication.W_ItemStart_time = DateUtil.currentTime2();
//        carItemStartModel.setDetectionItemStartDate(DateUtil.currentTime2());
        carItemStartModel.setDetectionItemStartDate(BaseApplication.W_ItemStart_time);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(carItemStartModel);
//        String upjson_url = "{[" + jsonStr.substring(1, jsonStr.length() - 1) + "]}";
        String upjson_url = "{" + jsonStr.substring(1, jsonStr.length() - 1) + "}";
        String logStr = "\n" + "外检项目开始----请求参数" + "\n" +
                "json::" + upjson_url + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + ITEM_START;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("getDataWaiJainStart---jsonStr==" + upjson_url);
        UtilsLog.e("getDataWaiJainStart---url==" + SharedPreferencesUtils.getIP(this) + ITEM_START);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + ITEM_START).tag(this)
                .upJson(upjson_url)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body().trim();
//                        UtilsLog.e("getDataWaiJainStart-result==" + result);
//                        UtilsLog.e("getDataWaiJainStart-result=22=" + result.substring(1, result.length() -
//                                1));
                        String logStr = "\n" + "外检项目开始----请求成功" + "\n" +
                                "result::" + response.body() + "\n" +
                                "URL::" + SharedPreferencesUtils.getIP(instances) + ITEM_START;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        CommonUtils.hideLoadingDialog(instances);
//                        String str = result.substring(1, result.length() - 1);
                        if (result.contains("ok") || result.contains("OK")) {
//                            updateCarStatusInfor(1);
//                        getDataReportData();
                            Intent intent = new Intent(instances, WaiJianPhotoActivity.class);
                            intent.putExtra("CarDetailsActivity_CarsInforModel", carsInforModel);
                            intent.putExtra("CarDetailsActivity_acCarDetailsWaijianchedao",
                                    acVehicleLine.getText().toString().trim());
                            intent.putExtra("CarDetailsActivity_Line", Line);
                            intent.putExtra("login_w_name", Login_name);
                            intent.putExtra("login_IDNumber", login_IDNumber);
                            startActivity(intent);
                            finish();
                        } else {
                            CommonUtils.showDialog(instances, "项目开始失败，请重试，如果还未成功请联系管理员");
//                            Toast.makeText(CarDetailsActivity.this, "上线失败，请重试", Toast.LENGTH_LONG)
//                                    .show();
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        String logStr2 = "\n" + "外检项目开始请求--onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("getDataWaiJainStart-onError==" + response.body());
                        CommonUtils.hideLoadingDialog(instances);
                    }
                });
    }

}
